/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GameSignInPanel.java
 *
 * Created on 2009-9-26, 11:25:43
 */
package com.res0w.SMMS.ui.login;

import com.res0w.SMMS.login.LoginAuth;
import com.res0w.SMMS.ui.UDResource.ImageBorder;
import com.res0w.SMMS.ui.UDResource.ImageButton;
import com.res0w.SMMS.ui.admin.AdminWindow;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

/**
 *
 * @author res0w
 * @since 2009-9-26
 * @version 0.1
 */
public class GameSignInPanel extends javax.swing.JPanel {

    private JFrame parentFrame;

    /** Creates new form GameSignInPanel */
    public GameSignInPanel(JFrame parentFrame) {
        myInit();
        initComponents();
        setPreferredSize(new Dimension(bg.getWidth(null), bg.getHeight(null)));
        laterInvoke();
        this.parentFrame = parentFrame;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        usrnameText = new javax.swing.JTextField();
        pwdText = new javax.swing.JPasswordField();
        signUpBtn = new javax.swing.JButton();
        signInBtn = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setFont(new java.awt.Font("微软雅黑", 0, 12));
        setPreferredSize(new java.awt.Dimension(888, 312));

        jLabel1.setFont(new java.awt.Font("微软雅黑", 0, 14));
        jLabel1.setText("用户名：");

        jLabel2.setFont(new java.awt.Font("微软雅黑", 0, 14));
        jLabel2.setText("密码：");

        usrnameText.setFont(new java.awt.Font("微软雅黑", 0, 14));

        pwdText.setFont(new java.awt.Font("微软雅黑", 0, 14));

        signUpBtn.setFont(new java.awt.Font("微软雅黑", 1, 14));
        signUpBtn.setForeground(java.awt.Color.red);
        signUpBtn.setText("现在报名！");
        signUpBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                signUpBtnActionPerformed(evt);
            }
        });

        signInBtn.setFont(new java.awt.Font("微软雅黑", 0, 14));
        signInBtn.setText("登陆");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(530, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(pwdText)
                            .addComponent(usrnameText, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(signUpBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(signInBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(125, 125, 125))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(105, 105, 105)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(usrnameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pwdText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(signUpBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(signInBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE))
                .addGap(52, 52, 52))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void signUpBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_signUpBtnActionPerformed
        // TODO add your handling code here:
        initTipsWindow();
    }//GEN-LAST:event_signUpBtnActionPerformed

//Create By Author
    private String imageURl = "/com/res0w/SMMS/ui/login/loginWindow.png";
    private ImageBorder border = new ImageBorder();
    private ImageButton btnClose;
    private Image bg;
    private SignUpTipsWindow tips;

    private void myInit() {
        try {
            bg = ImageIO.read(getClass().getResource(imageURl));
            border.setImage(bg);
            this.setBorder(border);
            setPreferredSize(new Dimension(bg.getWidth(null), bg.getHeight(null)));
            setLayout(null);

            btnClose = new ImageButton(
                    "/com/res0w/SMMS/ui/login/bclose_d.png",
                    "/com/res0w/SMMS/ui/login/bclose_h.png",
                    "/com/res0w/SMMS/ui/login/bclose_h.png");
            setButtonLocation(btnClose, 834, 8);
        } catch (IOException ex) {
            Logger.getLogger(GameSignInPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
                formMousePressed(e);
            }
        });
        addMouseMotionListener(new MouseAdapter() {

            @Override
            public void mouseDragged(MouseEvent e) {
                super.mouseDragged(e);
                formMouseDragged(e);
            }
        });
        btnClose.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

    private void laterInvoke() {
        signInBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    signInBtnListener();
                } catch (SQLException ex) {
                    Logger.getLogger(GameSignInPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    private void setButtonLocation(AbstractButton ab, int x, int y) {
        int width = ab.getIcon().getIconWidth();
        int height = ab.getIcon().getIconHeight();
        ab.setBounds(x, y, width, height);
        this.add(ab);
    }
//提供拖拽窗口移动的功能

    private void formMousePressed(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        pressedPoint = evt.getLocationOnScreen();
        //当在页面中点击的时候,tips消失
        if (tips != null) {
            tips.dispose();
        }
    }
    private Point pressedPoint;

    private void formMouseDragged(java.awt.event.MouseEvent evt) {
        Point draggedPoint = evt.getLocationOnScreen();
        Point location = SwingUtilities.getRoot(this).getLocationOnScreen();
        int x = draggedPoint.x - pressedPoint.x;
        int y = draggedPoint.y - pressedPoint.y;
        SwingUtilities.getRoot(this).setLocation(location.x + x, location.y + y);
        pressedPoint = draggedPoint;
        if (tips != null) {
            tips.dispose();
        }
    //提供tips基准坐标
    }

    private void initTipsWindow() {
        if (tips != null) {
            tips.dispose();
        }
        //提供当前窗口的坐标
        Point location = SwingUtilities.getRoot(this).getLocationOnScreen();
        //初始化tips模块
        tips = new SignUpTipsWindow();
        tips.setLocation(420 - tips.getWidth() + location.x, 240 + location.y);
        tips.pack();
        tips.setVisible(true);
//        tips.setAlwaysOnTop(true);
    }

    @SuppressWarnings("static-access")
    private void signInBtnListener() throws SQLException {
        String usrname = usrnameText.getText().toString().trim();
        String password = "";
        char[] pwd = pwdText.getPassword();
        for (int i = 0; i < pwd.length; i++) {
            password = password.trim() + pwd[i];
        }
        LoginAuth auth = new LoginAuth();
        switch (auth.login(usrname, password.trim())) {
            case -1:
                new JOptionPane().showMessageDialog(parentFrame, "密码验证错误！");
                break;
            case 0:
                //关闭tips窗口
                if (tips != null) {
                    tips.dispose();
                }
                AdminWindow sm = new AdminWindow();
                sm.setLocationRelativeTo(null);
                sm.setVisible(true);
                parentFrame.dispose();
                break;
        }

    }
    public boolean isOpened = false;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField pwdText;
    public javax.swing.JButton signInBtn;
    private javax.swing.JButton signUpBtn;
    private javax.swing.JTextField usrnameText;
    // End of variables declaration//GEN-END:variables
}
